%include 'boot.inc'


%macro IDT_ENTRY 2
section .text
intr_%1_entry:
	;if error code flag is set we do nothing cpu has push the errorcode for us	
	;if error code flag is not set we push 0 to stack keep the handler keep same
	%if(%2==ERROR_CODE_ON)
		nop
	%else
		push 0	
	%endif
		push ds
		push es
		push gs
		push fs
		pushad
		mov ax,K_DATA_SEL
		mov ds,ax
		mov es,ax
		;send EOI to master and savle PIC
		mov al,0x20
		out 0xa0,al
		out 0x20,al
		push %1	
		call [idt_table+%1*4]
		jmp intr_exit

section .data
	dd intr_%1_entry

%endmacro

;when start user process we should use intr_exit go to the user mode
global intr_exit
section .text
intr_exit:
	;skip vector num
	add esp,4
	popad
	pop fs
	pop gs
	pop es
	pop ds
	;skip error code
	add esp,4
	iret
;cpu put error code in stack
ERROR_CODE_ON equ 1
;cpu not will not put error code in stack
ERROR_CODE_OFF equ 0

extern put_str
global IDT_ENTRY_TAB
;idt_table define in other elf
extern idt_table

section .rodata
intr_promote: db "interrupt occur!",0xa,0

section .data
IDT_ENTRY_TAB:
;IDT entry %1 vector %2 error code flag

%assign vec_nm 0
%rep 0x80 
	%if(vec_nm==8 || vec_nm==17 || (vec_nm>=10 && vec_nm<=14))
		IDT_ENTRY vec_nm,ERROR_CODE_ON
	%else
		IDT_ENTRY vec_nm,ERROR_CODE_OFF
	%endif	

%assign vec_nm vec_nm+1
%endrep
